﻿using System;
using System.Collections.Generic;
using WmsApp.Report;
using WmsApp.Service;
using WmsApp.StaticData;
using WmsApp.Style;
using WmsApp.UI;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormOut : BaseForm
    {
        private readonly BaseGoodsCodeTypeObserver _goodsCodeTypeObserver;
        private readonly OutStoreBillController _outStoreBillController;

        public FormOut()
        {
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                _outStoreBillController = new OutStoreBillController(textEditBillID3,
                                                                     /*this.lookUpEditProvider3, this.textEditProviderName3,*/
                                                                     dateEditBillDate3,
                                                                     textEditInBillStateDesc3,
                                                                     gridControlEdit3,
                                                                     lookUpEditInStoreType3,
                                                                     textEditGetName3, textEditProjectNo3,
                                                                     textEditPurpose3,
                                                                     textEditRequisitionNo3,
                                                                     comboBoxEditDepartment3,
                                                                     lookUpEditGetID3,
                                                                     dateEditOutBillDate3,
                                                                     comboBoxEditGetID3);
                _outStoreBillController.CustomInStoreBillId = this.checkEditCustomerInStoreBillID;
                _outStoreBillController.FBorrowRepairEditor = borrowRepairEditorMain;
                _outStoreBillController.FProjectNoOrRepairTrade = labelControlProjectNoOrRepairTrade;
                try
                {
                    var subject = GoodsCodeTypeSubjectFactory.GetGoodsCodeTypeSubject();
                    _goodsCodeTypeObserver = new GoodsCodeTypeObserver("out", subject,
                                                                       _outStoreBillController.
                                                                           FRepositoryItemGridLookUpEdit, this);
                    subject.Attach(_goodsCodeTypeObserver);
                }
                catch
                {
                }
            }
        }

        public void NewStockTacking(List<StockTackingDtl> dtl)
        {
            _outStoreBillController.NewStockTacking(dtl);
        }

        public override string HeadText
        {
            get { return "出库管理"; }
        }

        public void SetTabPageIndex(int index)
        {
            xtraTabControlMain.SelectedTabPageIndex = index;
        }

        public void CmdEnableControl()
        {
            if (_outStoreBillController.BillState == Constant.BillStateDraft)
            {
                CmdSave3.Enabled = true;
                CmdNewDtl3.Enabled = true;
                CmdInAffirm3.Enabled = true;
                lookUpEditInStoreType3.Enabled = true;
                CmdExcelImport.Enabled = true;
                //this.lookUpEditProvider3.Enabled = true;
                //this.dateEditBillDate3.Enabled = true;
                dateEditOutBillDate3.Enabled = true;

                //added new controls
                comboBoxEditDepartment3.Enabled = true;
                textEditPurpose3.Enabled = true;
                textEditProjectNo3.Enabled = true;
                textEditRequisitionNo3.Enabled = true;
                lookUpEditGetID3.Enabled = true;
                comboBoxEditGetID3.Enabled = true;

                CmdDelete.Enabled = true;
                textEditGetName3.Enabled = true;
                textEditProjectNo3.Enabled = true;
                textEditPurpose3.Enabled = true;
                textEditRequisitionNo3.Enabled = true;
                comboBoxEditDepartment3.Enabled = true;
                lookUpEditGetID3.Enabled = true;

                borrowRepairEditorMain.SetControlEnable(true);
            }
            else
            {
                CmdSave3.Enabled = false;
                CmdNewDtl3.Enabled = false;
                CmdInAffirm3.Enabled = false;
                lookUpEditInStoreType3.Enabled = false;
                CmdExcelImport.Enabled = false;
                //this.lookUpEditProvider3.Enabled = false;
                //this.dateEditBillDate3.Enabled = false;
                dateEditOutBillDate3.Enabled = false;

                //added new controls
                comboBoxEditDepartment3.Enabled = false;
                textEditPurpose3.Enabled = false;
                textEditProjectNo3.Enabled = false;
                textEditRequisitionNo3.Enabled = false;
                lookUpEditGetID3.Enabled = false;
                comboBoxEditGetID3.Enabled = false;

                CmdDelete.Enabled = false;
                textEditGetName3.Enabled = false;
                textEditProjectNo3.Enabled = false;
                textEditPurpose3.Enabled = false;
                textEditRequisitionNo3.Enabled = false;
                comboBoxEditDepartment3.Enabled = false;
                lookUpEditGetID3.Enabled = false;

                borrowRepairEditorMain.SetControlEnable(false);
            }
        }

        #region 按钮事件

        private void CmdNew3_Click(object sender, EventArgs e)
        {
            _outStoreBillController.NewOutStoreBill();
            CmdEnableControl();
        }

        private void CmdNewDtl3_Click(object sender, EventArgs e)
        {
            _outStoreBillController.AddOutStoreBillDtl();
            CmdEnableControl();
        }

        private void CmdSave3_Click(object sender, EventArgs e)
        {
            _outStoreBillController.SaveOutStoreBill();
            CmdEnableControl();
        }

        private void CmdInAffirm3_Click(object sender, EventArgs e)
        {
            _outStoreBillController.AffirmOutStoreBill();
            _outStoreBillController.LoadOutStoreBill(_outStoreBillController.CurrentBillID);
            CmdEnableControl();
        }

        private void CmdNew_Click(object sender, EventArgs e)
        {
            _outStoreBillController.NewOutStoreBill();
            xtraTabControlMain.SelectedTabPage = xtraTabPage3;
            CmdEnableControl();
        }

        /// <summary>
        ///   查询入库单
        /// </summary>
        /// <param name = "sender"></param>
        /// <param name = "e"></param>
        private void CmdQuery_Click(object sender, EventArgs e)
        {
            var service =
                EntityServiceFactory.GetEntityService<OutStoreBill, OutStoreBillService>();
            IQueryControl queryControl = qOutStoreBill1;
            gridControlEdit1.DataSource = service.GetEntityList(queryControl.GetCondition());
            RowStyleConditionController.AddStyleFormatCondition(gridView1, "BillStateDesc", "[BillStateDesc] != '确认'");
        }

        private void CmdNew2_Click(object sender, EventArgs e)
        {
            _outStoreBillController.NewOutStoreBill();
            xtraTabControlMain.SelectedTabPage = xtraTabPage3;
            CmdEnableControl();
        }

        /// <summary>
        ///   查询入库明细
        /// </summary>
        /// <param name = "sender"></param>
        /// <param name = "e"></param>
        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            var service = EntityServiceFactory.GetEntityService<ExtendOutStoreBillDtl, ExtendOutStoreBillDtlService>();
            IQueryControl queryControl = qOutStoreBillDtl1;
            gridControlEdit2_1.DataSource = service.GetEntityList(queryControl.GetCondition());
        }

        private void CmdEdit1_Click(object sender, EventArgs e)
        {
            var obj = gridView1.GetFocusedRow();
            if (obj != null)
            {
                _outStoreBillController.LoadOutStoreBill(((OutStoreBill) obj).BillID);
                xtraTabControlMain.SelectedTabPage = xtraTabPage3;
                CmdEnableControl();
            }
        }

        private void CmdEdit2_Click(object sender, EventArgs e)
        {
            var obj = gridView2.GetFocusedRow();
            if (obj != null)
            {
                _outStoreBillController.LoadOutStoreBill(((OutStoreBillDtl) obj).BillID);
                xtraTabControlMain.SelectedTabPage = xtraTabPage3;
                CmdEnableControl();
            }
        }

        private void CmdDelete_Click(object sender, EventArgs e)
        {
            var obj = gridView3.GetFocusedRow();
            if (obj != null)
            {
                _outStoreBillController.RemoveOutStoreBillDtl((OutStoreBillDtl) obj);
                CmdEnableControl();
            }
        }

        private void CmdPrint_Click(object sender, EventArgs e)
        {
            _outStoreBillController.Print<ReportOutputOutStoreBill>();
        }

        private void CmdPrint2_Click(object sender, EventArgs e)
        {
            _outStoreBillController.Print<ReportOutputOutStoreBill借用单>();
        }

        private void CmdPrint3_Click(object sender, EventArgs e)
        {
            _outStoreBillController.Print<ReportOutputOutStoreBill送修单>();
        }

        private void CmdExcelImport_Click(object sender, EventArgs e)
        {
            _outStoreBillController.AddOutStoreBillDtlByImport(); 
        }

        #endregion
    }
}